package linklist;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Test;

import java.util.HashMap;
import java.util.LinkedList;

/**
 * @author kunkun
 * @className LeetCode_146
 * @Description LCR缓存
 * @date 2025/3/12 10:23
 */
public class LeetCode_146 {

    private class Node{
        Integer key ;
        Integer value ;
        Node left;
        Node right;

        public Node() {
        }

        public Node(Integer key,Integer value, Node left, Node right) {
            this.key = key;
            this.value = value;
            this.left = left;
            this.right = right;
        }
    }

    private Node head = new Node(); //头指针
    private Node end = new Node();  //尾指针
    private Integer currentSize;
    private HashMap<Integer,Node> map = new HashMap<>();
    private Integer maxSize ;

    //初始化链表
    public void initList(){
        head.right=end;
        end.left = head;
        end.right = null;
        currentSize=0;
    }

    public LeetCode_146(int capacity) {
        initList();
        maxSize=capacity;
    }

    public static void main(String[] args) {
        LeetCode_146 subject = new LeetCode_146(2);
        subject.put(1,1);
        subject.put(2,2);
        System.out.println(subject.get(1)); //1
        subject.put(3,3);
        System.out.println(subject.get(2));//-1
        subject.put(4,4);
        System.out.println(subject.get(1));//-1
        System.out.println(subject.get(3));//3
        System.out.println(subject.get(4));//4

/*        LeetCode_146 subject = new LeetCode_146(1);
        subject.put(2,1);
        System.out.println(subject.get(1)); //-1*/

    }


    public int get(int key) {
        if (!map.containsKey(key)){
            return -1;
        }else {
            //修改原指向
            Node node = map.get(key);
            node.left.right = node.right;
            node.right.left = node.left;

            //修改现指向
            head.right.left=node;
            node.right = head.right;
            head.right = node;
            node.left = head;

            return node.value;

        }

    }

    public void put(int key, int value) {
        //判断是否存在
        if (!map.containsKey(key)){     //不存在，则新增
            //判断是否超长
            if (currentSize<maxSize){   //小于，可以添加
                //插入
                Node node = new Node(key,value,head,head.right);
                head.right=node;
                node.right.left = node;
                //添加map
                map.put(key,node);
                //计数
                currentSize++;
            }else{      //大于，移除在添加
                //插入
                Node node = new Node(key,value,head,head.right);
                head.right=node;
                node.right.left = node;
                //end前移
                end.left.left.right=end;
                Node temp = end.left;
                end.left = end.left.left;
                //添加map，删除map
                map.put(key,node);
                map.remove(temp.key);
                temp=null;
            }
        }else {
            //修改值
            Node node = map.get(key);
            node.value=value;
            map.put(key,node);
            //修改原指向
            node.left.right = node.right;
            node.right.left = node.left;
            //修改现指向
            head.right.left=node;
            node.right = head.right;
            head.right = node;
            node.left = head;
        }
    }
}
